﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_navigation_list : System.Web.UI.Page
    {
        private Guid _applicationId;
        private Guid _selectedNavigationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                Response.Redirect("./application-list.aspx");
            }
            else
            {
                _applicationId = new Guid(Request.QueryString["id"]);

                if (!string.IsNullOrEmpty(Request.QueryString["navigationid"]))
                {
                    _selectedNavigationId = new Guid(Request.QueryString["navigationid"]);
                }

                if (!IsPostBack)
                {
                    ICoreService service = ServiceLocator.Get<ICoreService>();
                    ListResult<Navigation> navigations = service.GetNavigations(_applicationId);

                    foreach (var item in navigations.Data)
                    {
                        BindTree(_ctlTreeNavigation, item);
                    }

                    _ctlGridShortcut.DataSource = service.GetShortcuts(_applicationId);
                    _ctlGridShortcut.DataBind();
                }

                if (_selectedNavigationId != Guid.Empty)
                {
                    _ctlLinkAdd.NavigateUrl = string.Format("./application-navigation-add.aspx?applicationid={0}&parentid={1}", _applicationId.ToString("D"), _selectedNavigationId.ToString("D"));
                }
                else
                {
                    _ctlLinkAdd.NavigateUrl = string.Format("./application-navigation-add.aspx?applicationid={0}", _applicationId.ToString("D"));
                }
            }
        }

        #region Bind
        private void BindTree(TreeView tree, Navigation navigation)
        {
            TreeNode node = new TreeNode();
            node.Text = navigation.Caption;
            node.Value = navigation.Id.ToString("D");
            node.NavigateUrl = string.Format("./application-navigation-list.aspx?id={0}&navigationid={1}", _applicationId.ToString("D"), navigation.Id.ToString("D"));
            if (_selectedNavigationId != Guid.Empty && navigation.Id == _selectedNavigationId)
            {
                node.Selected = true;
            }
            tree.Nodes.Add(node);
            foreach (var item in navigation.Children)
            {
                BindTree(node, item);
            }
        }

        private void BindTree(TreeNode parentNode, Navigation navigation)
        {
            TreeNode node = new TreeNode();
            node.Text = navigation.Caption;
            node.Value = navigation.Id.ToString("D");
            node.NavigateUrl = string.Format("./application-navigation-list.aspx?id={0}&navigationid={1}", _applicationId.ToString("D"), navigation.Id.ToString("D"));
            if (_selectedNavigationId != Guid.Empty && navigation.Id == _selectedNavigationId)
            {
                node.Selected = true;
            }
            parentNode.ChildNodes.Add(node);

            foreach (var item in navigation.Children)
            {
                BindTree(node, item);
            }
        }
        #endregion

        #region 设置
        protected void _ctlBtnSetDefault_Click(object sender, EventArgs e)
        {
            if (_ctlTreeNavigation.SelectedNode != null)
            {
                Guid navigationId = new Guid(_ctlTreeNavigation.SelectedNode.Value);
                ICoreService service = ServiceLocator.Get<ICoreService>();
                service.SetDefaultNavigation(navigationId);
                Response.Redirect(Request.Url.OriginalString);
            }
        }

        protected void _ctlBtnSetShortcut_Click(object sender, EventArgs e)
        {
            if (_ctlTreeNavigation.SelectedNode != null)
            {
                Guid navigationId = new Guid(_ctlTreeNavigation.SelectedNode.Value);
                ICoreService service = ServiceLocator.Get<ICoreService>();
                Navigation navigation = service.GetNavigation(navigationId);
                service.AddShortcut(navigationId, navigation.Caption);
                Response.Redirect(Request.Url.OriginalString);
            }
        }
        #endregion

        protected void _ctlGridShortcut_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                Guid navigationId = new Guid(e.CommandArgument.ToString());
                ICoreService service = ServiceLocator.Get<ICoreService>();
                service.DeleteShortcut(navigationId);
                Response.Redirect(Request.Url.OriginalString);
            }
        }
    }
}